package com.cn.onetrip.adapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.cn.onetrip.activity.R;
import com.cn.onetrip.objects.NotesListObj;
import com.cn.onetrip.untility.DebugUtil;
import com.cn.onetrip.untility.SyncImageLoader;
import com.cn.onetrip.untility.SyncImageLoader.OnImageLoadListener;

public class TravelItemAdapter extends BaseAdapter {

	public Vector<NotesListObj> mModels = new Vector<NotesListObj>();
	private LayoutInflater mInflater;
	private Context mContext;
	private ListView mListView;
	private Handler mhandler;

	private List<ImageView> imageViewsList = new ArrayList<ImageView>();

	private SyncImageLoader syncImageLoader;

	public TravelItemAdapter(Context context, ListView listView, Handler handler) {
		mInflater = LayoutInflater.from(context);
		syncImageLoader = new SyncImageLoader(context);
		mContext = context;
		mListView = listView;
		mhandler = handler;
		mListView.setOnScrollListener(onScrollListener);
	}

	public void addNotes(String imageUrl, String title, String subTitle,
			String authorString, String time) {
		NotesListObj model = new NotesListObj();
		model.icon = imageUrl;
		model.title = title;
		model.subTitle = subTitle;
		model.author = authorString;
		model.time = time;
		mModels.add(model);
	}

	public void clean() {
		mModels.clear();
	}

	public void threadQuit() {
		if (syncImageLoader != null) {
			syncImageLoader.threadQuit();
		}
	}

	public void clear() {
		for (int i = 0; i < imageViewsList.size(); i++) {
			ImageView imageView = imageViewsList.get(i);
			imageView.setBackgroundDrawable(null);
			System.gc();
		}
		imageViewsList.clear();
	}

	@Override
	public int getCount() {
		return mModels.size();
	}

	@Override
	public Object getItem(int position) {
		if (position >= getCount()) {
			return null;
		}
		return mModels.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final NotesListObj model = mModels.get(position);
		
		final ViewHolder viewHolder;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.travel_list_items, null);

			viewHolder.Icon = (ImageView) convertView
					.findViewById(R.id.TravelImageBg);
			viewHolder.ContentTitle = (TextView) convertView
					.findViewById(R.id.TravelContentTitle);
			viewHolder.ContentWords = (TextView) convertView
					.findViewById(R.id.TravelContentWords);
			viewHolder.ContentAuthor = (TextView) convertView
					.findViewById(R.id.TravelContentAuthor);
			imageViewsList.add(viewHolder.Icon);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		
		viewHolder.ContentTitle.setText(model.title);
		viewHolder.ContentWords.setText(model.subTitle);
		viewHolder.ContentAuthor.setText(mContext.getResources().getString(
				R.string.travel_author)
				+ model.author);
		
		viewHolder.Icon.setBackgroundResource(R.drawable.default_image_small);
		syncImageLoader.loadImage(model.icon, new OnImageLoadListener() {

			@Override
			public void onImageLoad(Drawable drawable) {
				if (drawable != null) {
					viewHolder.Icon.setBackgroundDrawable(drawable);
				} else {
					viewHolder.Icon.setBackgroundResource(R.drawable.default_image_small);
				}
			}

			@Override
			public void onImageLoad(Integer t, Drawable drawable) {

			}

			@Override
			public void onError() {

			}

			@Override
			public void onError(Integer t) {

			}
		});
		return convertView;
	}

	public static Bitmap drawableToBitmap(Drawable drawable, int viewWidth,
			int viewHeight) {

		if (((BitmapDrawable) drawable).getBitmap() == null) {
			Log.d("Bitmap", "Bitmap = null");
			return null;
		}
		int imgWidth = ((BitmapDrawable) drawable).getBitmap().getWidth();
		int imgHeight = ((BitmapDrawable) drawable).getBitmap().getHeight();

		// Log.i("View size:", viewWidth+","+viewHeight);
		int x, y, w, h;
		if (imgWidth * viewHeight > viewWidth * imgHeight) {
			h = (int) imgHeight;
			w = (int) (imgHeight * viewWidth / viewHeight);
			y = 0;
			x = (imgWidth - w) / 2;
		} else {
			w = (int) imgWidth;
			h = (int) (imgWidth * viewHeight / viewWidth);
			y = (imgHeight - h) / 2;
			x = 0;
		}
		Bitmap.Config config = Bitmap.Config.RGB_565;
		Bitmap bitmap = Bitmap.createBitmap(
				((BitmapDrawable) drawable).getBitmap(), x, y, w, h);
		return bitmap;
	}

	public void loadImage() {
		int start = mListView.getFirstVisiblePosition();
		int end = mListView.getLastVisiblePosition();

		if (end >= getCount()) {
			end = getCount() - 1;
		}
		syncImageLoader.setLoadLimit(start, end);
		syncImageLoader.unlock();
	}

	AbsListView.OnScrollListener onScrollListener = new AbsListView.OnScrollListener() {

		@Override
		public void onScrollStateChanged(AbsListView view, int scrollState) {
			switch (scrollState) {
			case AbsListView.OnScrollListener.SCROLL_STATE_FLING:
				DebugUtil.debug("SCROLL_STATE_FLING");
				syncImageLoader.lock();
				break;
			case AbsListView.OnScrollListener.SCROLL_STATE_IDLE:
				DebugUtil.debug("SCROLL_STATE_IDLE");
				loadImage();
				// loadImage();
				break;
			case AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
				syncImageLoader.lock();
				break;

			default:
				break;
			}
		}

		@Override
		public void onScroll(AbsListView view, int firstVisibleItem,
				int visibleItemCount, int totalItemCount) {
		}
	};

	public static class ViewHolder {
		ImageView Icon;
		TextView ContentTitle;
		TextView ContentWords;
		TextView ContentAuthor;
	}
}
